package com.book.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.mapper.UserMapper;
import com.book.pojo.FavoritesVo;
import com.book.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
@Transactional
public class UserService extends ServiceImpl<UserMapper, User> {

    @Autowired
    private UserMapper userMapper;

    // 验证用户名 的存在性
    public String checkUser(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username); //查询目标 与 传入的数据
        User user = userMapper.selectOne(queryWrapper);

        if (user == null) {
            return "101";  //用户不存在
        } else {
            return "102";  //用户已存在
        }
    }

    // 登录验证  账号密码登录
    public String loginCheck(User loginUser, HttpSession session, HttpServletResponse response) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginUser.getUsername()); //查询用户
        User user = userMapper.selectOne(queryWrapper);

        if (user == null) {
            return "101"; //用户不存在
        } else {
            //判断密码是否正确
            if (loginUser.getPassword().equals(user.getPassword())) {
                session.setAttribute("user", user); //登录成功，把用户保存到session
                session.setMaxInactiveInterval(30 * 60); // 30分钟（默认30分钟）
                String id = session.getId();

                // 设置cookie
                Cookie cookie = new Cookie("JSESSIONID", id);
                cookie.setMaxAge(30 * 60); // 30分钟(默认0s)
                cookie.setPath("/");
                response.addCookie(cookie);
                return "100"; //密码正确
            } else {
                return "102"; //密码不正确
            }
        }
    }

    // 登录验证  手机号登录
    public String phoneLoginCheck(User loginUser, HttpSession session, HttpServletResponse response) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", loginUser.getPhone());
        User user = userMapper.selectOne(queryWrapper);

        if (user == null) {
            return "103";  // 手机号不存在
        } else {
            // 判断验证码是否正确
            String sysCode = (String) session.getAttribute(loginUser.getPhone());
            if (loginUser.getCode().equals(sysCode)) {
                session.setAttribute("user", user); //登录成功，把用户保存到session
                session.setMaxInactiveInterval(30 * 60); // 30分钟（默认30分钟）
                String id = session.getId();

                // 设置cookie
                Cookie cookie = new Cookie("JSESSIONID", id);
                cookie.setMaxAge(30 * 60); // 30分钟(默认0s)
                cookie.setPath("/");
                response.addCookie(cookie);
                return "100"; // 验证码正确
            } else {
                return "103"; // 验证码不正确
            }
        }
    }
}
